package mnstudent.businessLayer;

import mnstudent.dataacesslayer.SubjectDAL;
import mnstudent.datatranferobject.MajorOfSubjectDTO;
import mnstudent.datatranferobject.MajorOfSubjectDetailDTO;
import mnstudent.datatranferobject.OrderSemesterDTO;
import mnstudent.datatranferobject.PrerequisiteSubjectDTO;
import mnstudent.datatranferobject.PrerequisiteSubjectDetailDTO;
import mnstudent.datatranferobject.SubjectDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class SubjectBLL {

    SubjectDAL subjectDAL;

    public SubjectBLL() {
        subjectDAL = new SubjectDAL();
    }

    public ResultSet getAllSubject() {
        return subjectDAL.getAllSubject();
    }

    public ResultSet getAllMajor() {
        return subjectDAL.getAllMajorFromDatabase();
    }

    public ResultSet getAllOrderSemester() {
        return subjectDAL.getAllOrderSemesterFromDatabase();
    }

    /**
     * Lấy Danh sách môn học
     *
     * @return
     */
    public ArrayList<SubjectDTO> getAllSubjectToArray() {
        ArrayList<SubjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllSubject();
        try {
            while (rs.next()) {
                SubjectDTO state = new SubjectDTO(rs.getNString(1), rs.getNString(4), rs.getNString(2), rs.getInt(3));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Lấy Danh sách môn học
     *
     * @return
     */
    public ArrayList<PrerequisiteSubjectDTO> getAllPrerequisiteSubjectToArray() {
        ArrayList<PrerequisiteSubjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllPrerequisiteSubject();
        try {
            while (rs.next()) {
                PrerequisiteSubjectDTO state = new PrerequisiteSubjectDTO(rs.getNString(1), rs.getNString(2));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Lấy Danh Sách Ngành
     *
     * @return
     */
    public ArrayList<MajorOfSubjectDTO> getAllMajorToArray() {
        ArrayList<MajorOfSubjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllMajor();
        try {
            while (rs.next()) {
                MajorOfSubjectDTO state = new MajorOfSubjectDTO(rs.getNString(1), rs.getNString(2));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Hàm lấy danh sách Môn học tiên quyết theo ID môn học
     *
     * @param _id
     * @return
     */
    public ArrayList<PrerequisiteSubjectDTO> getListPrerequisiteSubjectOfSubject(String _id) {
        ArrayList<PrerequisiteSubjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllPrerequisiteSubjectOfSubjectByIdFromDatabase(_id);
        try {
            while (rs.next()) {
                PrerequisiteSubjectDTO obj = new PrerequisiteSubjectDTO(rs.getNString(1), rs.getNString(2));
                temp.add(obj);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    private ResultSet getAllPrerequisiteSubjectOfSubjectByIdFromDatabase(String _id) {
        return subjectDAL.getAllPrerequisiteSubjectOfSubjectByIdFromDatabase(_id);
    }
    
    /**
     * Phuong thuc lay danh sach mon hoc tien quyet cua 1 mon hoc theo ma mon hoc
     * @param _idSubject
     * @return 
     */
    public ArrayList<SubjectDTO> getAllPrerequisiteSubjectOfSubjectByIdFromDatabaseToArray(String _idSubject)
    {
        ArrayList<SubjectDTO> temp = new ArrayList<>();
        ResultSet rs = subjectDAL.getAllPrerequisiteSubjectOfSubjectByIdFromDatabase(_idSubject);
        try {
            while(rs.next())
            {
                SubjectDTO objSub = new SubjectDTO(rs.getNString(1), rs.getNString(2), rs.getNString(4), rs.getInt(3));
                temp.add(objSub);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }
    
    /**
     * Lấy danh sách Ngành của môn học theo ID Môn học
     *
     * @param _id
     * @return
     */
    public ArrayList<MajorOfSubjectDTO> getListMajorOfSubject(String _id) {
        ArrayList<MajorOfSubjectDTO> temp = new ArrayList<>();
        ResultSet rs = getAllMajorOfSubjectByIdFromDatabase(_id);
        try {
            while (rs.next()) {
                MajorOfSubjectDTO obj = new MajorOfSubjectDTO(rs.getNString(1), rs.getNString(2), rs.getNString(3));
                temp.add(obj);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    private ResultSet getAllMajorOfSubjectByIdFromDatabase(String _id) {
        return subjectDAL.getAllMajorOfSubjectByIdFromDatabase(_id);
    }

    private ResultSet getIDSubjectFromDatabase() {
        return subjectDAL.getIDSubjectFromDatabase();
    }

    /**
     * Lấy ID MONHOC vưa Insert
     *
     * @return
     */
    public String getIDSubject() {
        String temp = "";
        ResultSet rs = getIDSubjectFromDatabase();
        try {
            while (rs.next()) {
                temp = rs.getNString(1);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Lấy Danh sách thứ tụ học kỳ
     *
     * @return
     */
    public ArrayList<OrderSemesterDTO> getAllOrderSemesterToArray() {
        ArrayList<OrderSemesterDTO> temp = new ArrayList<>();
        ResultSet rs = getAllOrderSemester();
        try {
            while (rs.next()) {
                OrderSemesterDTO state = new OrderSemesterDTO(rs.getNString(1), rs.getNString(2));
                temp.add(state);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;
    }

    /**
     * Ham xoa mot MONHOC ra khoi database
     *
     * @param _subject
     * @return
     */
    public boolean deleteSubjectFromDatabase(SubjectDTO _subject) {
        return subjectDAL.deleteSubjectFromDatabase(_subject);
    }

    /**
     * Ham Them mot mon hoc vao database
     *
     * @param _subjectObj
     * @return
     */
    public boolean insertSubjectToDatabase(SubjectDTO _subjectObj) {
        return subjectDAL.insertSubjectToDatabase(_subjectObj);
    }

    public boolean insertPrerequisiteSubjectDetail(PrerequisiteSubjectDetailDTO subPrerequisiteSubjectObj) {
        return subjectDAL.insertPrerequisiteSubjectDetail(subPrerequisiteSubjectObj);
    }

    public boolean insertArrayPrerequisiteSubjectDetail(ArrayList<PrerequisiteSubjectDetailDTO> arr) {
        return subjectDAL.insertArrayPrerequisiteSubjectDetail(arr);
    }

    public boolean updateSubjectToDatabase(SubjectDTO subjectObj) {
        return subjectDAL.updateSubjectToDatabase(subjectObj);
    }

    public boolean deletePrerequisiteSubjectDetailFromDatabase(String subjectID) {
        return subjectDAL.deletePrerequisiteSubjectDetailFromDatabase(subjectID);
    }

    private ResultSet getAllPrerequisiteSubject() {
        return subjectDAL.getAllPrerequisiteSubject();
    }

    public boolean deleteMajorOfSubjectDetailFromDatabase(String subjectID) {
        return subjectDAL.deleteMajorOfSubjectDetailFromDatabase(subjectID);
    }

    public boolean insertMajorOfSubjectDetail(MajorOfSubjectDetailDTO subMajorOfSubjectObj) {
        return subjectDAL.insertMajorOfSubjectDetail(subMajorOfSubjectObj);
    }

    public boolean insertArrayMajorOfSubjectDetail(ArrayList<MajorOfSubjectDetailDTO> arr) {
        return subjectDAL.insertArrayMajorOfSubjectDetail(arr);
    }
}
